<?php

namespace app\common\model;

use think\Env;
use think\Model;

/**
 * 模型
 */
class GoodsImage extends Model
{

    public function setImageAttr($value)
    {
        if (!empty($value)){
            $arr = parse_url($value);
            return $arr['path'];
        }
        return '';
    }

    public function getImageAttr($value)
    {
        return !empty($value) ? Env::get('app.app_host') . $value : '';
    }

    public function goods()
    {
        return $this->belongsTo( Goods::class, 'goods_id');
    }

    public static function batchInsert($goodsId, $images)
    {
        foreach ($images as $image){
            self::insert([
                'goods_id' => $goodsId,
                'image' => $image
            ]);
        }
        return true;

    }

    /**
     * 获取商品首图
     * @param $goodsId
     * @return float|mixed|string
     */
    public static function getFirstImage($goodsId)
    {
        return self::where('goods_id', $goodsId)->value('image');
    }

}
